const users = [
	{id: 1, username: 'houxiaoqing', password: '0322'},
	{id: 2, username: 'lixiang', password: '0322'},
	{id: 3, username: 'xigua', password: '0322'},
	{id: 4, username: 'lizhi', password: '0322'},
]

export default defineEventHandler(async (event) => {
    try {
        // 解析请求体中的 JSON 数据
        const body = await readBody(event);

        // 模拟后端接口查询数据库
        // 实际上应直接发起HTTP请求，请求后端接口返回数据
        const un = body.username;
        const pw = body.password;
        for (let i = 0; i < users.length; i++) {
            const element = users[i];
            if(element.username != un) continue;
            else{
                if(element.password == pw){
                    return {code: 1, msg: '登录成功', data: body}
                }
                return {code: 1, msg: '密码错误', data: null};
            }
        }
        return {code: 1, msg: '不存在该用户', data: null};
        
        
      } catch (error) {
        // 错误处理
        return { error: 'Error parsing JSON body' };
      }
})